home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER3 / SETBTNSZ.C < prev    next >
C/C++ Source or Header  |  1996-03-06  |  7KB  |  217 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "setbtnsz.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "TB_SETBUTTONSIZE"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. // Toolbar buttons. 
  108. //.................
  109. TBBUTTON tbButtons[] = 
  110.     { 0, IDM_TEST1, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0},
  111.     { 1, IDM_TEST2, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0},
  112.     { 2, IDM_TEST3, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0},
  113.     { 3, IDM_TEST4, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0},
  114. }; 
  115.  
  116.  
  117. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  118. {
  119. static HWND hToolWnd   = NULL;
  120.  
  121.    switch( uMsg )
  122.    {
  123.       case WM_CREATE :
  124.               // Initialize the common control library
  125.               // and create the toolbar.
  126.               //.......................................
  127.               InitCommonControls();
  128.  
  129.               hToolWnd = CreateWindowEx( 0, TOOLBARCLASSNAME, NULL, 
  130.                                          WS_CHILD | WS_BORDER | WS_VISIBLE, 
  131.                                          0, 0, 0, 0, hWnd, (HMENU)101, hInst, NULL);
  132.  
  133.               if ( hToolWnd )
  134.               {
  135.                  HBITMAP     hBmp;
  136.                  TBADDBITMAP tb;
  137.                  int index, stdidx;
  138.  
  139.                  // Set the size of the TBBUTTON structure.
  140.                  //........................................
  141.                  SendMessage(hToolWnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0); 
  142.  
  143.                  // Set the button and bitmap size.
  144.                  //................................
  145.                  SendMessage(hToolWnd, TB_SETBUTTONSIZE, 0, MAKELPARAM( 30, 30 ));
  146.                  SendMessage(hToolWnd, TB_SETBITMAPSIZE, 0, MAKELPARAM( 20, 20 ));
  147.  
  148.                  // Load bitmap to add to the toolbar.
  149.                  //...................................
  150.                  hBmp = CreateMappedBitmap( hInst, 100, 0, NULL, 0 );
  151.  
  152.                  tb.hInst = NULL;
  153.                  tb.nID   = (UINT) hBmp;
  154.                  stdidx = SendMessage(hToolWnd, TB_ADDBITMAP, 4, (LPARAM)&tb);
  155.  
  156.                  for (index = 0; index < 4; index++)
  157.                     tbButtons[index].iBitmap += stdidx;
  158.  
  159.                  // Add the buttons to the toolbar.
  160.                  //................................
  161.                  SendMessage(hToolWnd, TB_ADDBUTTONS, 4, (LONG) &tbButtons[0]);
  162.  
  163.                  // Force the toolbar to size itself.
  164.                  //..................................
  165.                  SendMessage(hToolWnd, WM_SIZE, 0, 0 );
  166.               }
  167.               break;
  168.  
  169.       case WM_COMMAND :
  170.               switch( LOWORD( wParam ) )
  171.               {
  172.                  case IDM_ABOUT :
  173.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  174.                         break;
  175.  
  176.                  case IDM_EXIT :
  177.                         DestroyWindow( hWnd );
  178.                         break;
  179.               }
  180.               break;
  181.       
  182.       case WM_DESTROY :
  183.               PostQuitMessage(0);
  184.               break;
  185.  
  186.       default :
  187.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  188.    }
  189.  
  190.    return( 0L );
  191. }
  192.  
  193.  
  194. LRESULT CALLBACK About( HWND hDlg,           
  195.                         UINT message,        
  196.                         WPARAM wParam,       
  197.                         LPARAM lParam)
  198. {
  199.    switch (message) 
  200.    {
  201.        case WM_INITDIALOG: 
  202.                return (TRUE);
  203.  
  204.        case WM_COMMAND:                              
  205.                if (   LOWORD(wParam) == IDOK         
  206.                    || LOWORD(wParam) == IDCANCEL)    
  207.                {
  208.                        EndDialog(hDlg, TRUE);        
  209.                        return (TRUE);
  210.                }
  211.                break;
  212.    }
  213.  
  214.    return (FALSE); 
  215. }
  216.